package com.example.tool.bean.business.wechat;

import com.alibaba.fastjson.JSONArray;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class SpreadsheetUpdateRangeRequest {

   private String sheetId;
   private int startRow;
   private int startCol;
   private List<List<String>>cellDataVal=new ArrayList<>();


    public  com.alibaba.fastjson.JSONObject getUpdateRequest(String sheetId, int startRow, int startCol, List<List<String>> cellDataVal){
        List<List<CellData>> cellData = cellDataVal.stream().map(i ->
                i.stream().map(CellData::new).collect(Collectors.toList())
        ).collect(Collectors.toList());
        com.alibaba.fastjson.JSONObject request = new com.alibaba.fastjson.JSONObject();
        com.alibaba.fastjson.JSONObject updateRequest = new com.alibaba.fastjson.JSONObject();
        // 构建GridData
        com.alibaba.fastjson.JSONObject gridData = new com.alibaba.fastjson.JSONObject();
        gridData.put("start_row", startRow);
        gridData.put("start_column", startCol);
        JSONArray rows = new JSONArray();
        for (List<CellData> row : cellData) {
            com.alibaba.fastjson.JSONObject rowData = new com.alibaba.fastjson.JSONObject();
            JSONArray values = new JSONArray();
            for (CellData cell : row) {
                values.add(buildCellData(cell));
            }

            rowData.put("values", values);
            rows.add(rowData);
        }

        gridData.put("rows", rows);
        updateRequest.put("sheet_id", sheetId);
        updateRequest.put("grid_data", gridData);

        request.put("update_range_request", updateRequest);
        return request;
    }
    /**
     * 构建单个单元格数据
     */
    private static com.alibaba.fastjson.JSONObject buildCellData(CellData cell) {
        com.alibaba.fastjson.JSONObject cellData = new com.alibaba.fastjson.JSONObject();

        // 单元格值
        com.alibaba.fastjson.JSONObject cellValue = new com.alibaba.fastjson.JSONObject();
        if (cell.getLink() != null) {
            com.alibaba.fastjson.JSONObject link = new com.alibaba.fastjson.JSONObject();
            link.put("text", cell.getText());
            link.put("url", cell.getLink());
            cellValue.put("link", link);
        } else {
            cellValue.put("text", cell.getText());
        }
        cellData.put("cell_value", cellValue);

        // 单元格格式（可选）
        if (cell.getFormat() != null) {
            cellData.put("cell_format", buildCellFormat(cell.getFormat()));
        }

        return cellData;
    }
    /**
     * 构建单元格格式
     */
    private static com.alibaba.fastjson.JSONObject buildCellFormat(CellFormat format) {
        com.alibaba.fastjson.JSONObject formatObj = new com.alibaba.fastjson.JSONObject();
        com.alibaba.fastjson.JSONObject textFormat = new com.alibaba.fastjson.JSONObject();

        textFormat.put("font", format.getFont());
        textFormat.put("font_size", format.getFontSize());
        textFormat.put("bold", format.isBold());
        textFormat.put("italic", format.isItalic());

        if (format.getColor() != null) {
            com.alibaba.fastjson.JSONObject color = new com.alibaba.fastjson.JSONObject();
            color.put("red", format.getColor().getRed());
            color.put("green", format.getColor().getGreen());
            color.put("blue", format.getColor().getBlue());
            color.put("alpha", format.getColor().getAlpha());
            textFormat.put("color", color);
        }

        formatObj.put("text_format", textFormat);
        return formatObj;
    }
    @Data
    public static class CellData {
        private String text;
        private String link;
        private SpreadsheetUpdateRangeRequest.CellFormat format = new SpreadsheetUpdateRangeRequest.CellFormat();

        public CellData(String text) {
            this.text = text;
        }

        // 链式调用方法
        public SpreadsheetUpdateRangeRequest.CellData setLink(String url) {
            this.link = url;
            return this;
        }

        public SpreadsheetUpdateRangeRequest.CellData setBold(boolean bold) {
            this.format.setBold(bold);
            return this;
        }

        public SpreadsheetUpdateRangeRequest.CellData setFontSize(int size) {
            this.format.setFontSize(size);
            return this;
        }

        public SpreadsheetUpdateRangeRequest.CellData setColor(SpreadsheetUpdateRangeRequest.Color color) {
            this.format.setColor(color);
            return this;
        }

        // getters...
    }
    @Data
    public  static class CellFormat {
        private String font = "Arial";
        private int fontSize = 12;
        private boolean bold;
        private boolean italic;
        private SpreadsheetUpdateRangeRequest.Color color;

    }
    @Data
    public static class Color {
        private int red;
        private int green;
        private int blue;
        private int alpha = 255;

        public Color(int r, int g, int b) {
            this.red = r;
            this.green = g;
            this.blue = b;
        }

    }
}
